package com.cbd.util;

import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: CBD
 * @Date: 2024/11/5 15:50
 * @Description:
 * @param <T>
 */
@Data
public class ResultResponse<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private int code;
    private String message;
    private T data;

    /**
     * 成功返回
     * @return
     * @param <T>
     */
    public static <T> ResultResponse<T> success() {
        ResultResponse<T> resultResponse = new ResultResponse<>();
        resultResponse.setCode(StatusEnum.SUCCESS.getCode());
        resultResponse.setMessage(StatusEnum.SUCCESS.getMessage());
        return resultResponse;
    }
    /**
     * 成功返回
     * @param data 数据
     * @return
     * @param <T>
     */
    public static <T> ResultResponse<T> success(T data) {
        ResultResponse<T> resultResponse = new ResultResponse<>();
        resultResponse.setCode(StatusEnum.SUCCESS.code);
        resultResponse.setMessage(StatusEnum.SUCCESS.message);
        resultResponse.setData(data);
        return resultResponse;
    }

    /**
     * 自定义错误信息
     * @param statusEnum 状态枚举
     * @param message 信息
     */
    public static<T> ResultResponse<T> error(StatusEnum statusEnum, String message) {
        ResultResponse<T> resultResponse = error(statusEnum);
        resultResponse.setMessage(message);
        return resultResponse;
    }
    /**
     * 自定义错误信息
     * @param statusEnum 状态枚举
     */
    public static<T> ResultResponse<T> error(StatusEnum statusEnum) {
        ResultResponse<T> resultResponse = new ResultResponse<>();
        resultResponse.setCode(statusEnum.getCode());
        resultResponse.setMessage(statusEnum.getMessage());
        return resultResponse;
    }

    /**
     * 自定义错误信息
     * @param msg 信息
     */
    public static <T> ResultResponse<T> fail(String msg) {
        ResultResponse<T> response = new ResultResponse<>();
        response.setCode(403); // 使用403状态码表示禁止访问
        response.setMessage(msg);
        return response;
    }
}
